package com.keta.foundation.interceptor;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.keta.foundation.constants.Constants;
import com.keta.foundation.entity.LogInfo;
import com.keta.foundation.entity.Monitor;
import com.keta.foundation.service.MonitorService;
import com.keta.foundation.shiro.ShiroUser;
import com.keta.foundation.util.AopUtil;

public class ExecuteTimeInterceptor extends HandlerInterceptorAdapter {
	
	@Autowired
	private MonitorService monitorService;
	
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		long starTime = System.currentTimeMillis();
		AopUtil.putRequest(request);
		AopUtil.putArgs(Constants.START_EXETIME_ARGUMENTS, starTime);
		return super.preHandle(request, response, handler);
	}
	
	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
		long endTime = System.currentTimeMillis();
		AopUtil.putArgs(Constants.END_EXETIME_ARGUMENTS, endTime);
		super.postHandle(request, response, handler, modelAndView);
	}
	
	
	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
		
		if (!(handler instanceof HandlerMethod)) {
			return;
		}
		
		long after = System.currentTimeMillis();
		
		System.out.println(AopUtil.getArgs(Constants.END_EXETIME_ARGUMENTS));
		
		long startJava = (long)AopUtil.getArgs(Constants.START_EXETIME_ARGUMENTS);
		long endJava =  (long)AopUtil.getArgs(Constants.END_EXETIME_ARGUMENTS);
		
		final long javaTime  = endJava - startJava;
		final long tplTime  = after - endJava;
		final long exeTime  = after - startJava;
		String contextPath =  request.getContextPath();
		final String uri = request.getRequestURI().replace(contextPath, "");
		final String ip = request.getRemoteAddr();
		
		if(uri.equals("/monitor/index")){
			return ;
		}
		
		// 开启一个线程来写入日志，避免入库操作造成阻塞渲染视图
		new Thread(new Runnable() {

			@Override
			public void run() {
				
				ShiroUser shiroUser = (ShiroUser) SecurityUtils.getSubject().getPrincipal();
				String userName ="";
				if(shiroUser==null){
					userName = "未登陆";
				}else{
					userName = shiroUser.getLoginName();
				}
				
				Monitor monitor = new Monitor();
				monitor.setUri(uri);
				monitor.setExetime((int)exeTime);
				monitor.setJavaExetime((int)javaTime);
				monitor.setTplExetime((int)tplTime);
				monitor.setUsername(userName);
				monitor.setIp(ip);
				monitor.setCreateTime(new Date());
				monitorService.saveOrUpdate(monitor);
				
			}
		}).start();
		AopUtil.romeveRequest();
		super.afterCompletion(request, response, handler, ex);
	}
	
	
}
